package com.caoji.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caoji.advice.lyExcption;
import com.caoji.entity.SpecGroupDTO;
import com.caoji.entity.SpecParamDTO;
import com.caoji.item.entity.SpecGroup;
import com.caoji.item.mapper.SpecGroupMapper;
import com.caoji.item.service.SpecGroupService;
import com.caoji.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SpecGroupServiceImpl extends ServiceImpl<SpecGroupMapper, SpecGroup> implements SpecGroupService {
   @Autowired
    SpecParamService specParamService;
    @Override
    public List<SpecGroupDTO> querybycateoryid(Long id) {

        return SpecGroupDTO.convertEntityList(query().eq("category_id",id).list());
    }

    @Override
    public void addgroup(SpecGroupDTO groupDTO) {
        if (groupDTO!=null){
            baseMapper.insert(groupDTO.toEntity(SpecGroup.class));
        }else {
            throw new lyExcption(500,"添加失败");
        }
    }

    @Override
    public void updategroup(SpecGroupDTO groupDTO) {
        updateById(groupDTO.toEntity(SpecGroup.class));
    }

    @Override
    public List<SpecGroupDTO> querybycid(Long cid) {
        List<SpecGroup> specGroups = query().eq("category_id", cid).list();
        if (CollectionUtils.isEmpty(specGroups)){
            throw new lyExcption(404,"找不到");
        }
        List<SpecGroupDTO> specGroupDTOS = SpecGroupDTO.convertEntityList(specGroups);
        for (SpecGroupDTO specGroup : specGroupDTOS) {
            List<SpecParamDTO> slectall = specParamService.slectall(specGroup, cid);
            specGroup.setParams(slectall);
        }
        return specGroupDTOS;
    }

}